﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 软构大作业
{
    public partial class Form17 : Form
    {
        private new AutoAdaptWindowsSize AutoSize;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public Form17()
        {
            InitializeComponent();
            // 设置窗体的KeyPreview属性为true，以便窗体可以接收键盘事件
            this.KeyPreview = true;
            // 为窗体添加KeyDown事件处理
            this.KeyDown += Form17_KeyDown;
        }

        private void Form17_KeyDown(object sender, KeyEventArgs e)
        {
            // 检查是否按下Enter键
            if (e.KeyCode == Keys.Enter)
            {
                // 触发button1的点击事件
                button1.PerformClick();
                // 标记事件已处理，防止其他控件处理该事件
                e.Handled = true;
                e.SuppressKeyPress = true; // 防止发出"叮"的声音
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取用户输入的借阅期限和逾期费用
                int overdueDays = (int)numericUpDown1.Value;
                decimal overdueFees = numericUpDown2.Value;

                // 创建SQL更新语句
                string sql = $"UPDATE library_db.admins SET OverdueDays = {overdueDays}, OverdueFees = {overdueFees} WHERE admin_id IS NOT NULL";

                // 使用Data类执行更新
                Data dao = new Data();
                int rowsAffected = dao.Excute(sql);

                // 显示更新结果
                MessageBox.Show($"成功更新了借阅规则。", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                numericUpDown1.Value = 0;
                numericUpDown2.Value = 0;
                Global.Instance.form17login.Hide();
                Global.Instance.form7login.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新借阅规则时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    numericUpDown1.Value = 0;
        //    numericUpDown2.Value = 0;
        //    Global.Instance.form17login.Hide();
        //    Global.Instance.form7login.Show();
        //}

        private void Form17_FormClosed(object sender, FormClosedEventArgs e)
        {
            numericUpDown1.Value = 0;
            numericUpDown2.Value = 0;
            Global.Instance.form17login = null;
            Global.Instance.form7login.Show();
        }

        private void Form17_Load(object sender, EventArgs e)
        {
            AutoSize = new AutoAdaptWindowsSize(this);
            this.SizeChanged += new EventHandler(Form_SizeChanged);
        }
        private void Form_SizeChanged(object sender, EventArgs e)
        {//窗体大小改变事件
            if (AutoSize != null) // 一定加这个判断，电脑缩放布局不是100%的时候，会报错
            {
                AutoSize.FormSizeChanged();
            }
        }
    }
}
